package org.jeecg.modules.gis.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.geotools.geojson.geom.GeometryJSON;
import org.jeecg.modules.gis.entity.GisRoadNetwork;
import org.jeecg.modules.gis.service.IGisRoadNetworkService;
import org.jeecg.modules.gis.service.RouteService;
import org.jeecg.modules.gis.util.GeoJsonJavaBeanUtil;
import org.jeecg.modules.gis.util.GeometryTypeUtil;
import org.jeecg.modules.gis.util.network.NetWorkUtil;
import org.jeecg.modules.gis.util.network.RoadNetworkModel;
import org.jeecg.modules.gis.vo.RoutePraVo;

import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiLineString;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;


/**
* @Description: 路网后台
* @Author: jeecg-boot
* @Date:   2023-04-24
* @Version: V1.0
*/
@Slf4j
@RestController
@RequestMapping("/gis/gisRoadNetwork")
public class GisRoadNetworkController {
   @Autowired
   private IGisRoadNetworkService gisRoadNetworkService;
    @Autowired
    private RouteService routeService;
    @Autowired
    private GeometryTypeUtil geometryTypeUtil;
    @RequestMapping(value = "/getShortestRoute",method = RequestMethod.GET)
    public JSONObject getShortestRoute(RoutePraVo routePraVo) throws Exception {
        return routeService.getShortestRoute(routePraVo);
    }
    /**
     * 导入geoJson
     *
     * @return
     */
    @RequestMapping(value = "/inputGeoJson",method = RequestMethod.POST)
    public String inputGeoJson(@RequestBody JSONObject gJson) throws Exception {

        JSONArray features = gJson.getJSONArray("features");
        for (int i = 0; i < features.size(); i++) {
            JSONObject properties = features.getJSONObject(i).getJSONObject("properties");
            JSONObject geomJson = features.getJSONObject(i).getJSONObject("geometry");
            GeometryJSON gjson = new GeometryJSON(17);
            MultiLineString mpl = (MultiLineString) gjson.read(new StringReader(geomJson.toJSONString()));
            GisRoadNetwork gisRoadNetwork = new GisRoadNetwork();
            gisRoadNetwork.setGeom(mpl);
            gisRoadNetwork.setType(properties.getString("type"));
            gisRoadNetworkService.save(gisRoadNetwork);
        }
        return "数据导入成功";
    }
    /**
     * 根据起点终点获取最短路径
     *
     * @return
     */
    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public JSONObject getAll() throws IOException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        List<GisRoadNetwork> lis = gisRoadNetworkService.list();
        JSONObject ptFeatureCollectionJson = null;
        JSONArray features = new JSONArray();
        for (GisRoadNetwork road:lis) {
            features.add(GeoJsonJavaBeanUtil.beanToFeature(road));
        }
        ptFeatureCollectionJson = geometryTypeUtil.featureJsonToFeatureCollectionJson(features);
        return ptFeatureCollectionJson;
    }

}
